﻿/*
Copyright (c) 2009 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.ComponentModel;
using System.Windows.Forms;
using BrightIdeasSoftware;
using TeachMe.Application.Data;
using TeachMe.Application.Properties;

namespace TeachMe.Application.Controls
{
    public class PriorityComboBox : ComboBox, IControlValueProvider
    {
        private class PriorityItem
        {
            public Priority Priority { get; set; }
            public string Caption { private get; set; }

            public override string ToString()
            {
                return Caption;
            }
        }

        public PriorityComboBox()
        {
            DropDownStyle = ComboBoxStyle.DropDownList;
        }

        public void FillPriorities()
        {
            FillPriorities(Priority.VeryLow, Priority.Low, Priority.Normal, Priority.High);
        }

        public void FillPriorities(params Priority[] priorities)
        {
            if (Items.Count > 0)
            {
                return;
            }

            foreach (Priority priority in priorities)
            {
                switch (priority)
                {
                    case Priority.VeryLow:
                        Items.Add(new PriorityItem { Caption = Resources.VeryLowPriority, Priority = Priority.VeryLow });
                        break;
                    case Priority.Low:
                        Items.Add(new PriorityItem { Caption = Resources.LowPriority, Priority = Priority.Low });
                        break;
                    case Priority.Normal:
                        Items.Add(new PriorityItem { Caption = Resources.NormalPriority, Priority = Priority.Normal });
                        break;
                    case Priority.High:
                        Items.Add(new PriorityItem { Caption = Resources.HighPriority, Priority = Priority.High });
                        break;
                    default:
                        throw new InvalidOperationException();
                }
            }
        }

        [Browsable(false)]
        [Localizable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Priority Priority
        {
            get
            {
                PriorityItem selection = SelectedItem as PriorityItem;
                if (selection == null)
                {
                    throw new InvalidOperationException();
                }

                return selection.Priority;
            }
            set
            {
                foreach (PriorityItem priorityItem in Items)
                {
                    if (priorityItem.Priority == value)
                    {
                        SelectedItem = priorityItem;
                    }
                }
            }
        }

        public object GetValue()
        {
            return Text;
        }
    }
}
